/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * vwPlanner.java
 *
 * Created on 18-mei-2011, 14:25:14
 */
package tcnplanner;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;

/**
 *
 * @author Koen
 */
public class vwPlanner extends javax.swing.JPanel {

    private Controller controller;

    /** Creates new form vwPlanner */
    public vwPlanner() {
        initComponents();
        setSpinner();
        this.tableReservations.setAutoCreateRowSorter(true);
        this.tableConflictions.setAutoCreateRowSorter(true);
        controller = Main.controller;
        this.tableReservations.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {
                Reservation selected = (Reservation) tableReservations.getModel().getValueAt(tableReservations.getSelectedRow(), 0);
                vwInfoReserveringen.setReservation(selected);
            }
        });

        this.tableConflictions.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {
                Reservation selected = (Reservation) tableConflictions.getModel().getValueAt(tableConflictions.getSelectedRow(), 0);
                //dpWijzigen.setDate(selected.getReservationDate());
                vwInfoReserveringen.setReservation(selected);
            }
        });
        loadReservations();
        loadConflictedReservations();
        //model.addElement(controller.getAllReservations().get(1));        
    }

    private void loadReservations() {
        TableModel model = new ReservationTable(controller.getAllReservations());
        tableReservations.setModel(model);
    }

    private void loadConflictedReservations() {

        TableModel model = new ReservationTable(controller.getConflictedReservations());
        tableConflictions.setModel(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbPlanning = new javax.swing.JLabel();
        lbConflicten = new javax.swing.JLabel();
        plWijzigen = new javax.swing.JPanel();
        lbNieuweResDatum = new javax.swing.JLabel();
        lbNieuweResTijd = new javax.swing.JLabel();
        btWijzigRes = new javax.swing.JButton();
        spHoure = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        dpWijzigen = new org.jdesktop.swingx.JXDatePicker();
        vwInfoReserveringen = new tcnplanner.vwInfo();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableReservations = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableConflictions = new javax.swing.JTable();
        btConfirm = new javax.swing.JButton();
        btConfirmAll = new javax.swing.JButton();

        setBackground(java.awt.SystemColor.inactiveCaption);

        lbPlanning.setText("Reserveringen:");
        lbPlanning.setName("lbPlanning"); // NOI18N

        lbConflicten.setText("Conflicten in de planning:");
        lbConflicten.setName("lbConflicten"); // NOI18N

        plWijzigen.setBackground(new java.awt.Color(153, 153, 255));
        plWijzigen.setBorder(javax.swing.BorderFactory.createTitledBorder("Wijzigen Datum en Tijd"));
        plWijzigen.setName("pnWijzigenDatumTijd"); // NOI18N

        lbNieuweResDatum.setText("Nieuwe reservering datum:");
        lbNieuweResDatum.setName("jNieuweReserveringDatum"); // NOI18N

        lbNieuweResTijd.setText("Nieuwe reservering tijd:");
        lbNieuweResTijd.setName("jNieuweReserveringTijd"); // NOI18N

        btWijzigRes.setText("Wijzig reservering");
        btWijzigRes.setName("btWijzigenReservering"); // NOI18N
        btWijzigRes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btWijzigResActionPerformed(evt);
            }
        });

        spHoure.setName("spHoure"); // NOI18N

        jLabel1.setText("Uur");
        jLabel1.setName("jLabel1"); // NOI18N

        dpWijzigen.setDate(new Date());
        dpWijzigen.setName("dpWijzigen"); // NOI18N

        javax.swing.GroupLayout plWijzigenLayout = new javax.swing.GroupLayout(plWijzigen);
        plWijzigen.setLayout(plWijzigenLayout);
        plWijzigenLayout.setHorizontalGroup(
            plWijzigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(plWijzigenLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(plWijzigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbNieuweResDatum)
                    .addComponent(dpWijzigen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(plWijzigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btWijzigRes)
                    .addGroup(plWijzigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(plWijzigenLayout.createSequentialGroup()
                            .addComponent(spHoure, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(plWijzigenLayout.createSequentialGroup()
                            .addComponent(lbNieuweResTijd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(4, 4, 4))))
                .addGap(24, 24, 24))
        );
        plWijzigenLayout.setVerticalGroup(
            plWijzigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(plWijzigenLayout.createSequentialGroup()
                .addGroup(plWijzigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(plWijzigenLayout.createSequentialGroup()
                        .addComponent(lbNieuweResDatum)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dpWijzigen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(plWijzigenLayout.createSequentialGroup()
                        .addComponent(lbNieuweResTijd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(plWijzigenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spHoure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btWijzigRes, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        vwInfoReserveringen.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        vwInfoReserveringen.setName("vwInfoReserveringen"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tableReservations.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableReservations.setName("tableReservations"); // NOI18N
        tableReservations.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tableReservations);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tableConflictions.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableConflictions.setName("tableConflictions"); // NOI18N
        tableConflictions.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(tableConflictions);

        btConfirm.setText("Bevestig");
        btConfirm.setName("btConfirm"); // NOI18N
        btConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmActionPerformed(evt);
            }
        });

        btConfirmAll.setText("Bevestig allemaal");
        btConfirmAll.setName("btConfirmAll"); // NOI18N
        btConfirmAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btConfirm)
                        .addGap(118, 118, 118)
                        .addComponent(btConfirmAll))
                    .addComponent(lbPlanning)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vwInfoReserveringen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbConflicten)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 435, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(plWijzigen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(lbConflicten)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(plWijzigen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(vwInfoReserveringen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lbPlanning)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btConfirmAll, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btConfirm))
                        .addGap(21, 21, 21))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btWijzigResActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btWijzigResActionPerformed
        Reservation selected = (Reservation) tableConflictions.getModel().getValueAt(this.tableConflictions.getSelectedRow(), 0);

        if (selected != null) {
            Calendar dateplustijd = new GregorianCalendar();
            dateplustijd.setTime(this.dpWijzigen.getDate());
            dateplustijd.add(Calendar.HOUR_OF_DAY, Integer.parseInt(this.spHoure.getValue().toString()));
            selected.setReservationDate(dateplustijd.getTime());
            controller.editReservation(selected);
//        this.UpdateReservationRow(selected);
            this.loadConflictedReservations();
            this.loadReservations();
        } else {
            JOptionPane.showMessageDialog(this, "Select a reservation.", "No reservation selected", 1);
        }


    }//GEN-LAST:event_btWijzigResActionPerformed

    private void btConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmActionPerformed
        Reservation selected = (Reservation) tableReservations.getModel().getValueAt(this.tableReservations.getSelectedRow(), 0);
        if (selected != null) {
            selected.setStartDate();
            controller.editReservation(selected);
            this.loadReservations();
        }
    }//GEN-LAST:event_btConfirmActionPerformed

    private void btConfirmAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmAllActionPerformed

        Iterator it = controller.getAllReservations().iterator();
        boolean conflictedB = false;
        while (it.hasNext()) {
            Reservation next = (Reservation) it.next();
            ArrayList<Reservation> conflicted = this.controller.getConflictedReservations();
            boolean conflicts = false;
            if (next.getStatus() == Reservation.statusType.NEW) {
                for (Reservation r : conflicted) {
                    if (next.getId() == r.getId()) {
                        conflicts = true;
                        conflictedB = true;
                        break;
                    }
                    else
                    {
                        conflicts = false;
                    }
                }
            }
            if(!conflicts){
            next.setStartDate();
            controller.editReservation(next);
            }
        }
        if (conflictedB) {
            JOptionPane.showMessageDialog(this, "One or more reservations are conflicted", "Confirm reservations failed.", 1);
        }
        loadReservations();
    }//GEN-LAST:event_btConfirmAllActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConfirm;
    private javax.swing.JButton btConfirmAll;
    private javax.swing.JButton btWijzigRes;
    public org.jdesktop.swingx.JXDatePicker dpWijzigen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbConflicten;
    private javax.swing.JLabel lbNieuweResDatum;
    private javax.swing.JLabel lbNieuweResTijd;
    private javax.swing.JLabel lbPlanning;
    private javax.swing.JPanel plWijzigen;
    private javax.swing.JSpinner spHoure;
    private javax.swing.JTable tableConflictions;
    private javax.swing.JTable tableReservations;
    private tcnplanner.vwInfo vwInfoReserveringen;
    // End of variables declaration//GEN-END:variables

    private void setSpinner() {
        int houre = 12;
        SpinnerModel model = new SpinnerNumberModel(houre, //initial value
                houre - 12, //min
                houre + 11, //max
                1);

        this.spHoure.setModel(model);
    }
//    private void UpdateReservationRow(Reservation r)
//    {
//        TableModel model = tableReservations.getModel();
//        int rows =model.getRowCount();
//        for(int i = 0; i<rows;i++){
//            if(r ==(Reservation)model.getValueAt(rows, 0)){
//                model.setValueAt(r, rows, 0);
//            }
//            
//        }
//        
//        
//    }
}
